package org.kuan.lab.common.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.kuan.lab.base.controller.BaseBean;
import org.kuan.lab.base.util.PageDateModel;
import org.kuan.lab.common.entity.Role;
import org.kuan.lab.common.entity.User;
import org.kuan.lab.common.service.RoleService;
import org.primefaces.context.RequestContext;
import org.primefaces.model.LazyDataModel;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;


/**
 * Project: erp<br>
 * Description: Role Backing bean.<br>
 * Copyright (c) 2012<br>
 * All Rights Reserved.<br>
 * @author <a href="mailto:gly420@163.com">GuanLuYong</a><br>
 */
@Controller @Scope("view")
public class RoleBean extends BaseBean<Role> implements Serializable {
	private static final long serialVersionUID = 1L;
	@Resource
	private RoleService roleService;
	
	private List<User> users;
	private LazyDataModel<User> lazyRoleUsersData;
	
	// select user
	private User[] selectUsers;
	
	public void initAddRole() {
		instance = new Role();
		instance.setStatus(1l);
	}
	
	@Override
	public void search() {
		String prefix = "SELECT r ";
		String ttPrefix = "SELECT COUNT(r) ";
		StringBuilder suffix = new StringBuilder("FROM Role r WHERE 1=1 ");
		Object code = queryMap.get("code");
		if(code != null && !"".equals(code)) {
			suffix.append("AND r.code LIKE '" + code + "%' ");
		}
		Object name = queryMap.get("name");
		if(name != null && !"".equals(name)) {
			suffix.append("AND r.name LIKE '%" + name + "%' ");
		}
		String orderby = "ORDER BY r.code";
		lazyData = new PageDateModel<Role>(roleService, prefix, ttPrefix, suffix.toString(), orderby, null);
	}

	public void initRoleUsers() {
		users = roleService.getUsersByRoleId(this.instance.getId());
		lazyRoleUsersData = new PageDateModel<User>(users);
	}
	public void chooseUsers(User[] us) {
		// merge User
		for(User u : us) {
			boolean f = false;
			for(User u1 : users) {
				if(u.getId().compareTo(u1.getId()) == 0) {
					f = true;
					break;
				}
			}
			if(!f) {
				users.add(u);
			}
		}
	}
	public void deleteUsers() {
		if(selectUsers == null || selectUsers.length == 0) {
			FacesContext.getCurrentInstance().addMessage(null, 
					new FacesMessage(FacesMessage.SEVERITY_WARN, "移除角色关联的用户失败：", "请选择要移除掉的角色用户"));
			return;
		}
		
		users.removeAll(Arrays.asList(selectUsers));
		FacesContext.getCurrentInstance().addMessage(null, 
				new FacesMessage(FacesMessage.SEVERITY_INFO, "移除角色关联的用户成功：", "恭喜你！"));
	}
	public void communicateRoleUsers() {
//		if(users == null || users.isEmpty()) {
//			FacesContext.getCurrentInstance().addMessage(null, 
//					new FacesMessage(FacesMessage.SEVERITY_WARN, "角色关联用户：", "请选择要关联的用户，再保存。"));
//			return;
//		}
		roleService.communicateRoleUsers(users, instance);
		opSuccess("分配用户角色", null);
	}

	@Override
	public void editEntity() throws Exception {
		super.editEntity();
		roleService.update(instance);
	}

	@Override
	public void addEntity() throws Exception {
		super.addEntity();
		roleService.insert(instance);
	}

	@Override
	public void removeEntity() throws Exception {
		roleService.delete(instance);
	}
	
	// G & S
	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public LazyDataModel<User> getLazyRoleUsersData() {
		return lazyRoleUsersData;
	}

	public void setLazyRoleUsersData(LazyDataModel<User> lazyRoleUsersData) {
		this.lazyRoleUsersData = lazyRoleUsersData;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public User[] getSelectUsers() {
		return selectUsers;
	}

	public void setSelectUsers(User[] selectUsers) {
		this.selectUsers = selectUsers;
	}

}
